package com.view.purchasemanage;

import com.bean.Goods;
import com.bean.Purchase;
import com.bean.Role;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoComboBox;
import com.lovo.netCRM.component.LovoTxt;
import com.service.PowerService;
import com.service.PurchaseService;
import com.service.RoleService;
import com.service.UserService;
import com.service.impl.PowerServiceImpl;
import com.service.impl.PurchaseServiceImpl;
import com.service.impl.RoleServiceImpl;
import com.service.impl.UserServiceImpl;
import com.util.MyCheckBox;
import com.view.MainFrame;
import com.view.jurisdictionmanage.RoleAddDialog;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GoodsAddDialog extends JDialog {

    private LovoButton addBtn;
    private LovoButton cancelBtn;
    private LovoTxt nameTxt,manuTxt,buyTxt,sellTxt,numberTxt;
    private LovoComboBox<String> cbb;


    private MainFrame frame ;
    private PurchaseService purchaseService = new PurchaseServiceImpl();

    private Purchase purchase;

    public GoodsAddDialog(MainFrame frame) {
        // TODO Auto-generated constructor stub
        super(frame,"添加商品",true);
        this.frame = frame;
        this.purchase = frame.getpIMP().getPurchase();
        this.setBounds(350, 250, 520, 400);
        this.setLayout(null);
        cbbInit();
        this.init();
        this.setVisible(true);
    }

    public void cbbInit(){
        String[] strings = new String[]{"电脑产品","安防产品","打印扫描设备","办公耗材"};
        cbb = new LovoComboBox("类别",strings,270,20,this);
    }

    private void init() {
        // TODO Auto-generated method stub
        nameTxt = new LovoTxt("商品名称",20,20,this);
        manuTxt = new LovoTxt("厂家",20,80,this);

        buyTxt = new LovoTxt("建议采购价",20,140,this);
        sellTxt = new LovoTxt("建议零售价",20,200,this);
        numberTxt = new LovoTxt("数量",20,260,this);


        this.addBtn = new LovoButton("添加", 120, 320, this);
        this.cancelBtn = new LovoButton("取消", 270, 320, this);

        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String nameTxtText = nameTxt.getText();
                String manuTxtText = manuTxt.getText();
                int buyTxtText = Integer.parseInt(buyTxt.getText());
                int sellTxtText = Integer.parseInt(sellTxt.getText());
                String type = cbb.getItem();
                int numberTxtText = Integer.parseInt(numberTxt.getText());
                Goods goods = new Goods();
                goods.setName(nameTxtText);
                goods.setManufactor(manuTxtText);
                goods.setBuyPrice(buyTxtText);
                goods.setSellPrice(sellTxtText);
                goods.setType(type);
                if(purchaseService.addGoods(goods,numberTxtText,purchase)){
                    JOptionPane.showMessageDialog(null,"添加成功");
                    frame.flush();
                    GoodsAddDialog.this.dispose();
                }else{
                    JOptionPane.showMessageDialog(null,"添加失败");
                }
            }
        });

    }

}
